package com.xusheng.cli.controller.back;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.exceptionLogModule.ExceptionLog;
import com.xusheng.cli.service.ExceptionLogService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 异常日志表(ExceptionLog)表控制层
 *
 * @author jy
 * @since 2022-01-18 09:51:35
 */
@Validated
@RestController
@RequestMapping("/api/v1/exceptionLog")
public class ExceptionLogController {

	/**
	 * 服务对象
	 */
	@Resource
	private ExceptionLogService exceptionLogService;

	/**
	 * 查询所有数据
	 *
	 * @param exceptionLog 查询实体
	 * @return 所有数据
	 * @author jy
	 * @since 2022-01-18 09:51:35
	 */
	@GetMapping("queryAll")
	public Result<List<ExceptionLog>> queryAll(ExceptionLog exceptionLog) {
		return Result.success(exceptionLogService.list(new QueryWrapper<>(exceptionLog)));
	}

	/**
	 * 分页查询所有数据
	 *
	 * @param page         分页对象
	 * @param exceptionLog 查询实体
	 * @return 所有数据
	 * @author jy
	 * @since 2022-01-18 09:51:35
	 */
	@GetMapping("queryByPage")
	public Result<MyPage<ExceptionLog>> queryByPage(MyPage<ExceptionLog> page, ExceptionLog exceptionLog) {
		return Result.success(exceptionLogService.page(page, new QueryWrapper<>(exceptionLog)));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 * @author jy
	 * @since 2022-01-18 09:51:35
	 */
	@GetMapping("queryOne/{id}")
	public Result<ExceptionLog> selectOne(@PathVariable Serializable id) {
		return Result.success(exceptionLogService.getById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param exceptionLog 实体对象
	 * @return 新增结果
	 * @author jy
	 * @since 2022-01-18 09:51:35
	 */
	@PostMapping("insert")
	public Result<Object> insert(@Validated(Save.class) @RequestBody ExceptionLog exceptionLog) {
		return Result.isSuccess(exceptionLogService.save(exceptionLog));
	}

	/**
	 * 修改数据
	 *
	 * @param exceptionLog 实体对象
	 * @return 修改结果
	 * @author jy
	 * @since 2022-01-18 09:51:35
	 */
	@PutMapping("update")
	public Result<Object> update(@Validated(Update.class) @RequestBody ExceptionLog exceptionLog) {
		return Result.isSuccess(exceptionLogService.updateById(exceptionLog));
	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 * @author jy
	 * @since 2022-01-18 09:51:35
	 */
	@DeleteMapping("delete/{id}")
	public Result<Object> delete(@PathVariable Long id) {
		ExceptionLog exceptionLog = new ExceptionLog();
		exceptionLog.setId(id);
		return Result.isSuccess(exceptionLogService.removeById(exceptionLog));
	}

	/**
	 * 批量删除数据
	 *
	 * @param idList 主键结合
	 * @return 删除结果
	 * @author jy
	 * @since 2022-01-18 09:51:35
	 */
	@DeleteMapping("batchDelete")
	public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
		return Result.isSuccess(exceptionLogService.batchDelete(idList, new ExceptionLog()));
	}

}

